--****************************************************************************
--Table: LOAITAIKHOAN
--****************************************************************************

--select
if exists(select name from sysobjects where name='sp_Select_LoaiTaiKhoan')
drop proc sp_Select_LoaiTaiKhoan
go
create proc sp_Select_LoaiTaiKhoan
as 
begin
	select * from LOAITAIKHOAN
end

--insert
if exists(select name from sysobjects where name='sp_Insert_LoaiTaiKhoan')
drop proc sp_Insert_LoaiTaiKhoan
go
create proc sp_Insert_LoaiTaiKhoan
	@TenLoaiTaiKhoan nvarchar(11)
as 
begin
	begin tran
	declare @MaLoaiTaiKhoan int
	set @MaLoaiTaiKhoan=1
	while exists(select * from LOAITAIKHOAN where MaLoaiTaiKhoan=@MaLoaiTaiKhoan)
		set @MaLoaiTaiKhoan=@MaLoaiTaiKhoan+1

	if(exists(select * from LOAITAIKHOAN where TenLoaiTaiKhoan=@TenLoaiTaiKhoan))
	begin
		raiserror (N'Tên loại tài khoản này đã có trong CSDL!!!', 16,1)
		rollback tran
		return
	end
	else
	begin
		insert into LOAITAIKHOAN values(@MaLoaiTaiKhoan,@TenLoaiTaiKhoan)
		commit tran	
	end
end

--delete
if exists(select name from sysobjects where name='sp_Delete_LoaiTaiKhoan')
drop proc sp_Delete_LoaiTaiKhoan
go
create proc sp_Delete_LoaiTaiKhoan
	@MaLoaiTaiKhoan int
as 
begin
	begin tran
	if not exists(select* from LOAITAIKHOAN where MaloaiTaiKhoan=@MaloaiTaiKhoan)
	begin
		raiserror (N'Thông tin cần xoá không tồn tại!!!',16,1)
		rollback tran
		return
	end
	else 
		begin 
			Delete from LOAITAIKHOAN where MaLoaiTaiKhoan = @MaLoaiTaiKhoan
			commit tran			
		end 
end

--Update
if exists(select name from sysobjects where name='sp_Update_LoaiTaiKhoan')
drop proc sp_Update_LoaiTaiKhoan
go
create proc sp_Update_LoaiTaiKhoan
	@MaLoaiTaiKhoan int,
	@TenLoaiTaiKhoan nvarchar(11)
as 
begin
	begin tran
		Update LOAITAIKHOAN set TenLoaiTaiKhoan = @TenLoaiTaiKhoan
		where MaLoaiTaiKhoan = @MaLoaiTaiKhoan
		commit tran	
end


